﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace Widget.Lib
{
         
        public class WebEncrypter
        {
           static WebEncrypter _inner;
            public static WebEncrypter Current
            {
                get
                {
                    if (_inner == null)
                    {
                        _inner = new WebEncrypter();
                    }
                    return _inner;
                }
            }

            private  string encryptionKey = "1234567890123451";

            public  string Decrypt(string CipherText)
            {
                return Decrypt(CipherText, encryptionKey);
            }

            protected  string Decrypt(string CipherText, string EncryptionPrivateKey)
            {
                if (String.IsNullOrEmpty(CipherText))
                    return CipherText;

                TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
                tDESalg.Key = new ASCIIEncoding().GetBytes(EncryptionPrivateKey.Substring(0, 16));
                tDESalg.IV = new ASCIIEncoding().GetBytes(EncryptionPrivateKey.Substring(8, 8));

                byte[] buffer = HttpServerUtility.UrlTokenDecode(CipherText);
                string result = DecryptTextFromMemory(buffer, tDESalg.Key, tDESalg.IV);
                return result;
            }

            public  string Encrypt(string PlainText)
            {
                return Encrypt(PlainText, encryptionKey);
            }

            protected  string Encrypt(string PlainText, string EncryptionPrivateKey)
            {
                if (String.IsNullOrEmpty(PlainText))
                    return PlainText;

                TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
                tDESalg.Key = new ASCIIEncoding().GetBytes(EncryptionPrivateKey.Substring(0, 16));
                tDESalg.IV = new ASCIIEncoding().GetBytes(EncryptionPrivateKey.Substring(8, 8));

                byte[] encryptedBinary = EncryptTextToMemory(PlainText, tDESalg.Key, tDESalg.IV);
                string result = HttpServerUtility.UrlTokenEncode(encryptedBinary);
                return result;
            }

            private  byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV)
            {
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), CryptoStreamMode.Write);
                byte[] toEncrypt = new UnicodeEncoding().GetBytes(Data);
                cStream.Write(toEncrypt, 0, toEncrypt.Length);
                cStream.FlushFinalBlock();
                byte[] ret = mStream.ToArray();
                cStream.Close();
                mStream.Close();
                return ret;
            }

            private  string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
            {
                MemoryStream msDecrypt = new MemoryStream(Data);
                CryptoStream csDecrypt = new CryptoStream(msDecrypt, new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), CryptoStreamMode.Read);
                StreamReader sReader = new StreamReader(csDecrypt, new UnicodeEncoding());
                return sReader.ReadLine();
            }

            public class Login
            {
                public static string GetToken(params object[] parts)
                {
                    string token = parts[0].ToString();
                    for (int i = 1; i < parts.Length; i++)
                    {
                        token += "#" + parts[i].ToString();
                    }
                    return WebEncrypter.Current.Encrypt(token);
                }
                public static string [] GetParts(string token)
                {
                    string stringToken = WebEncrypter.Current.Decrypt(token);
                    return stringToken.Split("#".ToArray());
                }
            }
        }
    
}